import 'package:cloud_medicine/http/config.dart';
import 'package:cloud_medicine/http/shop_api.dart';
import 'package:cloud_medicine/model/base_user_info.dart';
import 'package:cloud_medicine/model/shop_detail.dart';
import 'package:cloud_medicine/pages/shopInfo_edit.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:url_launcher/url_launcher.dart';

class ShopInfo extends StatefulWidget {
  final num shopid;
  final bool isManage;
  ShopInfo({Key key, this.shopid, this.isManage = false}) : super(key: key);

  @override
  _ShopInfoState createState() => _ShopInfoState();
}

class _ShopInfoState extends State<ShopInfo> {
  ShopItem shopItem;
  bool _loading = false;
  Future<Null> _init() async {
    setState(() {
      _loading = true;
    });
    dynamic res = await getShopInfo_api(
        '$backIP/shop/getShopInfo', {'ID': widget.shopid}); //接口路径
    shopItem = ShopItem.init(res['data']);
    if (mounted) {
      setState(() {
        _loading = false;
      });
    }

    return;
  }

  @override
  void initState() {
    super.initState();
    _init();
  }

  _launchPhone() async {
    var url = 'tel:${shopItem.phonenumber}';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  _launchMap(String address) async {
    var url =
        "baidumap://map/geocoder?src=andr.baidu.openAPIdemo&address=$address";
    bool canLaunchUrl = await canLaunch(url);

    if (!canLaunchUrl) {
      Fluttertoast.showToast(
        msg: '未安装百度地图',
        backgroundColor: Colors.black,
        textColor: Colors.white,
      );
      return 'false';
    }
    await launch(url);
    return canLaunchUrl;
  }

  Widget _appBar() {
    if (_loading) {
      return AppBar(title: CircularProgressIndicator());
    } else {
      return AppBar(
        title: shopItem.name != null ? Text(shopItem.name) : Text('加载中'),
        centerTitle: true,
        actions: [
          user == null || user.identify.id == 2 || user.identify.id == 3
              ? Container(
                  margin: EdgeInsets.all(0),
                  child: IconButton(
                      padding: EdgeInsets.all(0),
                      color: shopItem.isCollect
                          ? Colors.yellow[500]
                          : Colors.white,
                      icon: shopItem.isCollect
                          ? Icon(Icons.star)
                          : Icon(Icons.star_border),
                      onPressed: () {
                        setState(() {
                          shopItem.isCollect =
                              !shopItem.isCollect; //TODO:将id传给后端
                        });
                      }))
              : widget.isManage
                  ? Container(
                      child: IconButton(
                        icon: Icon(Icons.mode_edit),
                        tooltip: '修改信息',
                        onPressed: () {
                          Navigator.of(context).push(MaterialPageRoute(
                              builder: (BuildContext context) => ShopInfoEdit(
                                    shopID: widget.shopid,
                                    phone: int.parse(shopItem.phonenumber),
                                    name: shopItem.name ?? 'asd',
                                    address: shopItem.address,
                                    delivery: shopItem.deliveryFee,
                                    inidelivery: shopItem.iniDeliveryFee,
                                    describe: shopItem.describe,
                                  )));
                        },
                      ),
                    )
                  : Container(),
        ],
      );
    }
  }

  Widget _childLayout() {
    if (_loading || shopItem == null) {
      return Center(
        child: Container(
          child: CircularProgressIndicator(),
        ),
      );
    } else {
      return Column(
        children: [
          Container(
              margin: EdgeInsets.only(top: 7, bottom: 2),
              color: Colors.white,
              child: ListTile(
                onTap: () {
                  _launchMap(shopItem.address);
                },
                title: Text(shopItem.address),
                leading: Icon(Icons.location_on),
                trailing: IconButton(
                  icon: Icon(Icons.phone),
                  onPressed: () {
                    showModalBottomSheet(
                        context: context,
                        builder: (context) => Container(
                              height: 0.27 * MediaQuery.of(context).size.height,
                              color: Colors.grey[300],
                              child: Column(
                                children: [
                                  Container(
                                    height: 47,
                                    margin: EdgeInsets.only(bottom: 2),
                                    color: Colors.white,
                                    child: ListTile(
                                      enabled: false,
                                      title: Center(
                                        child: Text('联系电话'),
                                      ),
                                    ),
                                  ),
                                  Container(
                                      color: Colors.white,
                                      margin: EdgeInsets.only(bottom: 5),
                                      child: ListTile(
                                        onTap: () {
                                          _launchPhone();
                                        },
                                        title: Center(
                                          child: Text(
                                              'Tel:${shopItem.phonenumber}'),
                                        ),
                                      )),
                                  Container(
                                      color: Colors.white,
                                      child: ListTile(
                                        onTap: () {
                                          Navigator.of(context).pop();
                                        },
                                        title: Center(
                                          child: Text('取消'),
                                        ),
                                      )),
                                ],
                              ),
                            ));
                  },
                ),
              )),
          Container(
              color: Colors.white,
              child: ListTile(
                title: Text(shopItem.describe),
                leading: Icon(Icons.description),
              )),
          Container(
            color: Colors.white,
            margin: EdgeInsets.only(top: 5),
            child: ListTile(
              leading: Icon(Icons.volume_up),
              title: Text('欢迎光临，很高兴为您服务~'),
            ),
          ),
          Container(
            color: Colors.white,
            margin: EdgeInsets.only(top: 2),
            child: ListTile(
              leading: Icon(Icons.list),
              title: Text(
                  '起送费￥${shopItem.iniDeliveryFee}   配送费￥${shopItem.deliveryFee}'),
            ),
          ),
        ],
      );
    }
  }

  // static Map<dynamic, dynamic> _shopDetail = {
  //   'address': '象山街道学府路301号江苏大学',
  //   'name': '益禾堂烧仙草',
  //   'describe': 'describe',
  //   'phonenumber': 8008208820,
  //   'grade': 9.7,
  //   'iniDeliveryFee': 20,
  //   'deliveryFee': 5,
  //   'isCollect': true,
  // };
  // bool _isCollect = shopItem.isCollect;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.grey[300],
        appBar: _appBar(),
        body: _childLayout());
  }
}
